import pickle
import hexfield
import AStar
import sqlite_utils
class CodeMap:
  def __init__(self):
    self.codes = []
  def build(self,r,c,code):
    self.row = r
    self.column = c
    self.codes = [code]*self.row*self.column
  
  def get(self,x,y):
    if(x<0)or(x>self.row)or(y>self.column)or(y<0):
      raise Exception('Out of boundaries')
    return self.codes[x*self.row+y]
  
  def get(self,x,y):
    if(x<0)or(x>self.row)or(y>self.column)or(y<0):
      raise Exception('Out of boundaries')
    return self.codes[x*self.row+y]
  
  def set(self,x,y,value):
    if(x<0)or(x>self.row)or(y>self.column)or(y<0):
      return
    
    self.codes[x*self.row+y] = value
  
  def write(self,file):
    pickle.dump(self.column,file)
    pickle.dump(self.row,file)
    pickle.dump(self.codes,file)
    
  def read(self,file):
    self.column = pickle.load(file)
    self.rows = pickle.load(file)
    self.codes = pickle.load(file)

#@DESC:
#@TODO:
class Tile:
  def __init__(self):
    self.type = None
    self.terrain = []
    
#@DESC:
#@TODO:    
class Map:
  def __init__(self,name,size = (32,32)):
    self.map = []
    self.size = size
    self.tiles_with_squads = []
    self.tiles = []
    self.visible = True
    self.hexfield = hexfield.Hexfield('field','graphics/hextiles.png',8,size)
    self.hexfield.build()
    self.hexfield.set_opacity(60)
    self.hexfield.subscribe_to()
    
    self.modifiers =  zip(*sqlite_utils.sql_exec("""SELECT wheel FROM terrains ORDER by code ASC"""))[0]
    
    for i in range(size[0]*size[1]):
      self.tiles.append(Tile())
    self.code_map = CodeMap()
    self.code_map.build(size[0],size[1],1)
    
    self.pathfinding = AStar.AStar(AStar.SQ_MapHandler(self,32,32))
  
  def save(self,file):
    f = open(file,"wb")
    self.code_map.write(f)
  
  def load(self,file):
    f = open(file,"rb")
    self.code_map.read(f)
    for c in range(self.code_map.column):
      for r in range(self.code_map.row):
        self.hexfield._set_code(c,r,self.code_map.get(c,r))
  
  def get_quickest_path(self,type,a,b):
    tmp = self.pathfinding.findPath(AStar.SQ_Location(a[0],a[1]),AStar.SQ_Location(b[0],b[1]),type)
    ret = []
    if not tmp is None:
      for el in tmp.getNodes():
        ret.append((el.location.x,el.location.y))
    
    return ret
    
  def _set_visible(self,val):
    self._visible = val
    if val:
      self.hexfield.unblock()
    else:
      self.hexfield.block()
      
  def _get_visible(self,val):
    return self._visible
  
  visible = property(_get_visible,_set_visible)
  
  def set_code(self,x,y,val):
    x,y = self.hexfield.tile_pos(x,y)
    if x < 0 or y <0  or x > self.size[0] or y > self.size[1]:
      return
  
    self.code_map.set(x,y,val)
    self.hexfield._set_code(x,y,val)
  
  def draw(self):
    self.hexfield.draw()
    
  def get(self,x,y):
      return self.code_map.get(x,y)    
  
  def get_code(self,x,y,type):
    a =  int(self.get(x,y))
    return int(100.0/self.modifiers[a])
